home *** CD-ROM | disk | FTP | other *** search
Text File | 1990-06-29 | 3.0 KB | 108 lines | [TEXT/GEOL] |
- Item 5059760 27-June-90 10:10PDT
-
- From: PERRY.G Gregg, Perry
-
- To: MACAPP.TECH$ MacApp Technical
-
- cc: MACAPP.NEWS$ MacApp News
-
- Sub: FailureHandler Class
-
- C++ MacAppers:
-
- Check out Scott Collins' FailureHandler class based on a Jeff Alger's
- suggestion. It reduces implementing exception handling in your C++ MacApp
- application to two lines of code (well almost).
-
- Scott's code looks like real C++ to me. He knows how to use it. So don't be
- scared at first when you can't quite figure out what he is doing (read his
- comments in the header file). Once you figure it out you'll see a few
- excellent examples of minimal C++ usage (meaning, one line doing the work of
- three; see if you can spot the magic line). Hint: "Think construction."
-
- The example on how to actually use it was a little hard for me to understand so
- I've included a sample of my own below.
-
- I never was,
- Perry 'Loose Energy' Gregg
-
- // Do one of these for handles if needed.
- // We can get more creative and create a set of useful classes for this
- exception task in the future.
- // in YourFile.h
- //*****************************************************************************
- *************
- // T P t r E r r o r H a n d l e r
- //*****************************************************************************
- *************
-
- //-----------------------------------------------------------------------------
- ---------------------
- class TPtrErrorHandler : public FailureHandler
- {
- public:
- // this constructor sets up pointers to the handles I might need to
- // dispose
- TPtrErrorHandler( Ptr *aPtr );
-
- public:
- // beef
- virtual voidRecover( short error, long message );
-
- private:
- Ptr *fPtr;
- };
-
- // in YourFile.cp
- //-----------------------------------------------------------------------------
- ---------------------
- #pragma segment AClose
- TPtrErrorHandler::TPtrErrorHandler( Ptr *aPtr )
- { fPtr = aPtr; }
-
-
-
- //-----------------------------------------------------------------------------
- ---------------------
- #pragma segment AClose
- void TPtrErrorHandler::Recover( short error, long message )
- {
- if ( fPtr != nil )
- DisposeIfPtr( *fPtr );
- }
-
- // the way you use
- //-----------------------------------------------------------------------------
- ---------------------
- #pragma segment AWriteFile
-
- pascal void CopyFilePiece(int fromRefNum, int toRefNum, long amount, int
- bufSize)
- {
- longamtLeft;
- longcount;
- int readErr;
- int writeErr;
- Ptr aBufPtr;
-
- aBufPtr = NewPtr(bufSize);
- FailNIL(aBufPtr);
- amtLeft = amount;
-
- // equivalent to new; plus, passes args to constructor
- TPtrErrorHandler myHandler( &aBufPtr );
-
- while (amtLeft > 0)
- {
- count = Min(amtLeft, bufSize);
- readErr = FSRead(fromRefNum, &count, aBufPtr);
- FailOSErr(readErr);
- writeErr = FSWrite(toRefNum, &count, aBufPtr);
- FailOSErr(writeErr);
- amtLeft = amtLeft - bufSize;
- };
- DisposPtr(aBufPtr);
- myHandler.Revoke();
- }
-
-